package com.lagou.client;

import com.lagou.model.Student;
import com.lagou.model.User;
import com.lagou.model.UserMessage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lu
 * @Date: 2021/10/1 16:29
 * @Description:实现客户端的主界面绘制和相应功能的实现
 */
public class ClientView {
    /**
     * 为了可以使用输入输出流，采用合成复用原则实现
     */
    private ClientInitClose cic;

    /**
     * 通过构造方法实现成员初始化
     * @param cic
     */
    public ClientView(ClientInitClose cic){
        this.cic = cic;
    }
    /**
     * 自定义成员方法实现客户端主界面的绘制
     */
    public void clientMainPage() throws IOException {
        while (true) {
            System.out.println("    \t在线考试系统");
            System.out.println("---------------------------------");
            System.out.print("    [1] 学员系统");
            System.out.println("    [2] 管理员系统");
            System.out.println("    [0] 退出系统");
            System.out.println("---------------------------------");
            System.out.println("请输入业务编号：");
            //Scanner sc = new Scanner(System.in);
            //int choose = sc.nextInt();
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    System.out.println("正在进入学员系统。。。");
                    break;
                case 2:
                    System.out.println("正在进入管理员系统。。。");
                    clientManagerLogin();
                    clientManagerSystem();
                    break;
                case 0:
                    System.out.println("正在退出系统。。。");
                    return;
                default:
                    System.out.println("输入错误，请重新选择");
            }
        }
    }

    private void clientManagerLogin() throws IOException {
        System.out.println("请输入管理员的账号信息");
        String username = ClientScanner.getScanner().next();
        System.out.println("请输入管理员的密码信息");
        String password = ClientScanner.getScanner().next();
        UserMessage u1 = new UserMessage("managerCheck",new User(username,password));
        cic.getOos().writeObject(u1);
        System.out.println("客户端发送管理员账户信息成功");
    }
    private void clientManagerSystem(){
        while(true){
            System.out.println("    \t管理员系统");
            System.out.println("---------------------------------");
            System.out.print("    [1] 学员管理模块");
            System.out.println("    [2] 考题管理模块");
            System.out.println("    [0] 退出管理员系统");
            System.out.println("---------------------------------");
            System.out.println("请输入模块编号：");
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose){
                case 1:
                    System.out.println("进入学员管理模块.....");
                    studentManager();
                    break;
                case 2:
                    System.out.println("进入考题管理模块.....");
                    break;
                case 0:
                    System.out.println("正在退出管理员系统。。。");
                    return;
                default:
                    System.out.println("输入错误，请重新选择");
            }
        }

    }
    private void studentManager(){
        List<Student> classes = new ArrayList<>();
        while (true){
            System.out.println("    \t学员管理模块");
            System.out.println("---------------------------------");
            System.out.print("    [1] 添加学员");
            System.out.println("    [2] 修改学员");
            System.out.print("    [3] 删除学员");
            System.out.println("    [4] 查询学员");
            System.out.println("    [0] 退出当前界面");
            System.out.println("---------------------------------");
            System.out.println("请输入编号：");
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose){
                case 1:
                    addStudent(classes);
                    break;
                case 2:
                    alterStudent(classes);
                    break;
                case 3:
                    deleteStudent(classes);
                    break;
                case 4:
                    showStudent(classes);
                    break;
                case 0:
                    System.out.println("正在退出管理员系统。。。");
                    return;
                default:
                    System.out.println("输入错误，请重新选择");
            }
        }
    }

    private void addStudent(List<Student> classes){
        Student student = new Student();
        message(student);
        classes.add(student);
        System.out.println("添加学生成功！！！");
    }
    private void alterStudent(List<Student> classes) {
        System.out.println("请输入修改学生的学号：");
        int id = ClientScanner.getScanner().nextInt();
        for (int i = 0; i < classes.size(); i++) {
            if (classes.get(i).getId() == id){
                System.out.println("该学生信息如下：");
                System.out.println(classes.get(i).toString());
                Student student = new Student();
                message(student);
                classes.set(i,student);
                System.out.println("修改学生信息成功！！！");
                break;
            }
        }
    }

    private void message(Student student) {
        System.out.println("请输入学生编号：");
        student.setId(ClientScanner.getScanner().nextInt());
        System.out.println("请输入学生姓名：");
        student.setName(ClientScanner.getScanner().next());
        System.out.println("请输入学生性别：");
        student.setSex(ClientScanner.getScanner().next());
        System.out.println("请输入学生年龄：");
        student.setAge(ClientScanner.getScanner().nextInt());
    }

    private void deleteStudent (List<Student> classes){
        System.out.println("请输入要删除学生的学号：");
        int id = ClientScanner.getScanner().nextInt();
        for (int i = 0; i < classes.size(); i++) {
            if(classes.get(i).getId()==id){
                classes.remove(i);
                System.out.println("删除学生成功！！！");
                break;
            }
        }
    }

    private void showStudent(List<Student> classes) {
        System.out.println("请输入查询学生的学号：");
        int id = ClientScanner.getScanner().nextInt();
        for (Student aClass : classes) {
            if (aClass.getId() == id) {
                System.out.println("学号\t姓名\t性别\t年龄");
                System.out.println(aClass.getId()+"\t"+aClass.getName()+"\t"+aClass.getSex()+"\t"+aClass.getAge());
                break;
            }
        }
    }

}
